import MovieClip = egret.MovieClip;
class AnimationWin extends BaseWin {
	private _btn: eui.Button;
	private roleMc: MovieClip
	private list: joyUI.JTileList;
	public constructor() {
		super();
		this.initMovie();

		this._btn = new eui.Button();
		this.addChild(this._btn);
		this._btn.x = 400;
		this._btn.y = 100;
		this._btn.label = "测试动画";
		this._btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTouchBtn, this);

		//设置列表有滚动条
		var screr: eui.Scroller = new eui.Scroller();
		this.addChild(screr);
		screr.scrollPolicyV = eui.ScrollPolicy.ON;
		screr.scrollPolicyH = eui.ScrollPolicy.OFF;

		this.list = new joyUI.JTileList();
		//this.addChild(this.list);		
		this.list.scrollEnabled = true;
		var dp: eui.ArrayCollection = new eui.ArrayCollection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]);
		this.list.dataProvider = dp;

		screr.viewport = this.list;
		screr.x = 0;
		screr.y = 50;
		//设置滚动器的尺寸 也是viewport对象的显示区域尺寸
		screr.width = 312;
		screr.height = 218;
		screr.viewport.validateSize();
		screr.viewport.validateNow();
		screr.viewport.scrollV = 150;

		var f: boolean;
		var st: number = egret.getTimer();
		this.list.addEventListener(egret.Event.RENDER, (e: egret.Event) => {
			var et: number = egret.getTimer() - st;	
			//console.log("渲染完成...", this.list.scrollV, 'et', et, this.list.constructor['name']);
			if (!f) {
				f = true;							
				//this.list.scrollV = 150; //每次设置 dataProvider 和拖动滚动条 都会触发该事件
			}
		}, this);

		this.list.addEventListener(egret.Event.RESIZE, (e: egret.Event) => {
			var et: number = egret.getTimer() - st;
			console.log("尺寸重置完成..", this.list.scrollV, 'et', et);
			this.list.scrollV = 150; //
		}, this);

	}

	private onTouchBtn(e: egret.TouchEvent): void {
		console.log("this.roleMc.currentFrame", this.roleMc.currentFrame);
		this.roleMc.gotoAndPlay(1);
		this.list.scrollToRow(2);
		this.createGameScene();
	}

	private webSocket: egret.WebSocket;
	private createGameScene(): void {
		if(!this.webSocket){
			this.webSocket = new egret.WebSocket();
			this.webSocket.addEventListener(egret.ProgressEvent.SOCKET_DATA, this.onReceiveMessage, this);
			this.webSocket.addEventListener(egret.Event.CONNECT, this.onSocketOpen, this);
			this.webSocket.type = egret.WebSocket.TYPE_BINARY;
		}
		if(!this.webSocket.connected){
			this.webSocket.connect("127.0.0.1", 9000);
		}else{
			this.onSocketOpen();
		}
		
	}
	private onSocketOpen(): void {
		var cmd = "Hello Egret WebSocket";
		console.log("连接成功，发送数据：" + cmd+';cmd.length'+cmd.length);
		var buf:egret.ByteArray = new egret.ByteArray();
		buf.writeUTF(cmd);
		//this.webSocket.writeUTF(cmd);
		this.webSocket.writeBytes(buf);
	}
	private onReceiveMessage(e: egret.Event): void {
		var buf:egret.ByteArray = new egret.ByteArray();	
		buf.endian = egret.Endian.LITTLE_ENDIAN;	
		this.webSocket.readBytes(buf,0);
		buf.position = 0;
		console.log('buf.bytesAvailable=',buf.bytesAvailable,'buf.position',buf.position);
		console.log('readShort',buf.readShort())
		console.log('readInt',buf.readInt())
		var msg = buf.readUTFBytes(buf.bytesAvailable)
		console.log("收到数据：" + msg);
	}

	private initMovie(): void {
		//创建mc动画的过程
		var mcData: any = RES.getRes("animation_json");
		var t: egret.Texture = RES.getRes('animation_png');
		var mcDataFactory = new egret.MovieClipDataFactory(mcData, t);
		var data: egret.MovieClipData = mcDataFactory.generateMovieClipData('attack');
		this.roleMc = new MovieClip(data);
		this.addChild(this.roleMc);

		this.roleMc.x = 180;
		this.roleMc.y = 300;
		console.log("roleMc.totalFrames", this.roleMc.totalFrames);
	}


}